#include "BG_Oled_N64.h"
#include "BG_Oled_Choose.h"
#include <U8x8lib.h>
#include <Arduino.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif


void BG_Oled_N64::beginOLED()
{
	u8x8.begin();
	u8x8.setPowerSave(0);
	u8x8.setFont(u8x8_font_chroma48medium8_r);
}

void BG_Oled_N64::setStr(uint8_t x, uint8_t y, const char* msg)
{
	u8x8.drawString(x, y, msg);
}

void BG_Oled_N64::cleanStr(uint8_t x, uint8_t y)
{
	u8x8.drawString(x, y, " ");
}

void BG_Oled_N64::cleanAll()
{
	u8x8.clear();
}

void  BG_Oled_N64::cleanOLED(uint8_t i)
{
	u8x8.drawString(0, i, "                ");
}

void BG_Oled_N64:: cleanAllOLED()
{
	u8x8.drawString(0, 4, "                ");
	u8x8.drawString(0, 5, "                ");
	u8x8.drawString(0, 6, "                ");
	u8x8.drawString(0, 7, "                ");
}

uint8_t BG_Oled_N64:: lenStrOLED(const char* payload)
{
	uint8_t len = strlen(payload);
	if (len < 64)
		return len;
	else
		return 64;
}

const char* BG_Oled_N64:: useString(String str)
{
	return str.c_str();
}

uint8_t BG_Oled_N64:: xyNum(uint8_t num)
{
	if (num <17&&num>0)
		return num-1;
	else
		return 0; 
}

void BG_Oled_N64:: setcOLED(const char* payload, uint8_t length)
{
	String ss = "", ss2 = "", ss3 = "", ss4 = "";
	if (length <= 16)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < length; i++)
		{
			ss += (char)payload[i];
			//Serial.print((char)payload[i]);
		}
		u8x8.drawString(0, 4, ss.c_str());
	}
	else if (length <= 32)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < 16; i++)
		{
			// Serial.print((char)payload[i]);
			ss += (char)payload[i];
		}
		//u8x8.drawString(0, 4, "                ");
		u8x8.drawString(0, 4, ss.c_str());
		for (int i = 16; i < length; i++)
		{
			// Serial.print((char)payload[i]);
			ss2 += (char)payload[i];
		}
		//u8x8.drawString(0, 5, "                ");
		u8x8.drawString(0, 5, ss2.c_str());
	}
	else if (length <= 48)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < 16; i++)
		{
			//Serial.print((char)payload[i]);
			ss += (char)payload[i];
		}
		//u8x8.drawString(0, 4, "                ");
		u8x8.drawString(0, 4, ss.c_str());
		for (int i = 16; i < 32; i++)
		{
			// Serial.print((char)payload[i]);
			ss2 += (char)payload[i];
		}
		//u8x8.drawString(0, 5, "                ");
		u8x8.drawString(0, 5, ss2.c_str());
		for (int i = 32; i < length; i++)
		{
			//Serial.print((char)payload[i]);
			ss3 += (char)payload[i];
		}
		//u8x8.drawString(0, 6, "                ");
		u8x8.drawString(0, 6, ss3.c_str());
	}
	else if (length <= 64)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < 16; i++)
		{
			//Serial.print((char)payload[i]);
			ss += (char)payload[i];
		}
		//u8x8.drawString(0, 4, "                ");
		u8x8.drawString(0, 4, ss.c_str());
		for (int i = 16; i < 32; i++)
		{
			//Serial.print((char)payload[i]);
			ss2 += (char)payload[i];
		}
		//u8x8.drawString(0, 5, "                ");
		u8x8.drawString(0, 5, ss2.c_str());
		for (int i = 32; i < 48; i++)
		{
			//Serial.print((char)payload[i]);
			ss3 += (char)payload[i];
		}
		//u8x8.drawString(0, 6, "                ");
		u8x8.drawString(0, 6, ss3.c_str());
		for (int i = 48; i < length; i++)
		{
			/// Serial.print((char)payload[i]);
			ss4 += (char)payload[i];
		}
		//u8x8.drawString(0, 7, "                ");
		u8x8.drawString(0, 7, ss4.c_str());
	}
}


void BG_Oled_N64:: setbOLED(byte* payload, uint8_t length)
{
	String ss = "", ss2 = "", ss3 = "", ss4 = "";
	if (length <= 16)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < length; i++)
		{
			ss += (char)payload[i];
			// Serial.print((char)payload[i]);
		}
		u8x8.drawString(0, 4, ss.c_str());
	}
	else if (length <= 32)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < 16; i++)
		{
			//Serial.print((char)payload[i]);
			ss += (char)payload[i];
		}
		//u8x8.drawString(0, 4, "                ");
		u8x8.drawString(0, 4, ss.c_str());
		for (int i = 16; i < length; i++)
		{
			//Serial.print((char)payload[i]);
			ss2 += (char)payload[i];
		}
		//u8x8.drawString(0, 5, "                ");
		u8x8.drawString(0, 5, ss2.c_str());
	}
	else if (length <= 48)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < 16; i++)
		{
			//Serial.print((char)payload[i]);
			ss += (char)payload[i];
		}
		//u8x8.drawString(0, 4, "                ");
		u8x8.drawString(0, 4, ss.c_str());
		for (int i = 16; i < 32; i++)
		{
			//Serial.print((char)payload[i]);
			ss2 += (char)payload[i];
		}
		//u8x8.drawString(0, 5, "                ");
		u8x8.drawString(0, 5, ss2.c_str());
		for (int i = 32; i < length; i++)
		{
			//Serial.print((char)payload[i]);
			ss3 += (char)payload[i];
		}
		//u8x8.drawString(0, 6, "                ");
		u8x8.drawString(0, 6, ss3.c_str());
	}
	else if (length <= 64)
	{
		ss = "";
		ss2 = "";
		ss3 = "";
		ss4 = "";
		cleanAllOLED();
		for (int i = 0; i < 16; i++)
		{
			// Serial.print((char)payload[i]);
			ss += (char)payload[i];
		}
		//u8x8.drawString(0, 4, "                ");
		u8x8.drawString(0, 4, ss.c_str());
		for (int i = 16; i < 32; i++)
		{
			// Serial.print((char)payload[i]);
			ss2 += (char)payload[i];
		}
		//u8x8.drawString(0, 5, "                ");
		u8x8.drawString(0, 5, ss2.c_str());
		for (int i = 32; i < 48; i++)
		{
			// Serial.print((char)payload[i]);
			ss3 += (char)payload[i];
		}
		//u8x8.drawString(0, 6, "                ");
		u8x8.drawString(0, 6, ss3.c_str());
		for (int i = 48; i < length; i++)
		{
			//Serial.print((char)payload[i]);
			ss4 += (char)payload[i];
		}
		//u8x8.drawString(0, 7, "                ");
		u8x8.drawString(0, 7, ss4.c_str());
	}
}
